package com.jimikongjian.service;

import com.jimikongjian.dao.IRoom;
import com.jimikongjian.dao.IRoomFollower;
import com.jimikongjian.models.Message;
import com.jimikongjian.models.ResponseMessage;
import com.jimikongjian.models.Room;
import com.jimikongjian.models.RoomFollower;
import com.jimikongjian.service.base.IRoomFollowerService;
import com.jimikongjian.service.base.IRoomService;
import com.jimikongjian.service.base.IUserService;
import com.jimikongjian.service.message.ResponseMessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import javax.validation.constraints.Max;
import java.util.List;

/**
 * Created by fuxiuyang on 17/3/14.
 */
@Service("roomFollowerService")
public class RoomFollowerService implements IRoomFollowerService {


    @Autowired
    @Qualifier("responseMessageService")
    private ResponseMessageService responseMessageService;


    @Autowired
    @Qualifier("iRoomFollowerImpl")
    private IRoomFollower iRoomFollowerImpl;

    @Autowired
    @Qualifier("iRoomImpl")
    private IRoom iRoomImpl;

    @Autowired
    @Qualifier("userService")
    private IUserService userService;


    @Autowired
    @Qualifier("roomService")
    private IRoomService roomService;

    /**
     * 关注某一个空间
     *
     * @param roomFollower
     * @return
     */
    public ResponseMessage followerRoom(RoomFollower roomFollower) {
        if (roomFollower.getUserId() > 0
                && roomFollower.getRoomId() > 0){
            if (userService.hasUser(roomFollower.getUserId())){
                if (roomService.hasRoom(roomFollower.getRoomId())){
                    if (!iRoomFollowerImpl.hasUser(roomFollower.getUserId(), roomFollower.getRoomId())){
                        /**
                         * 该服务是直接对外的接口，所以，只有空间为公共类型才可以
                         */
                        Room room = iRoomImpl.getRoomById(roomFollower.getRoomId());
                        if (room.getType() == Room.TYPE_PUBLIC_ROOM){
                            iRoomFollowerImpl.insertRoomFollower(roomFollower);
                            return responseMessageService.makeMessage(Message.SUCCESS);
                        }
                        return responseMessageService.makeMessage(Message.NO_PERMISSION_ERROR);
                    }
                    return responseMessageService.makeMessage(Message.PARAMS_ERROR);
                }
                return responseMessageService.makeMessage(Message.NO_HAVE_ROOM);
            }
            return responseMessageService.makeMessage(Message.NO_HAVE_USER);
        }
        return responseMessageService.makeMessage(Message.PARAMS_ERROR);
    }

    /**
     * 通过空间的id，查找该空间内的所有的用户的id号
     *
     * @param roomId
     * @return
     */
    public ResponseMessage selectRoomFollowerByRoomId(int roomId, int userId, int start, int length) {
        if (roomId > 0 && userId > 0 && start >= 0 && length > 0){
            if (roomService.hasRoom(roomId)){
                if (hasUser(userId, roomId)){
                    List list = iRoomFollowerImpl.getRoomFollowerListByRoomId(roomId, start, length);
                    return responseMessageService.makeMessage(list, Message.SUCCESS);
                }
                return responseMessageService.makeMessage(Message.NO_PERMISSION_ERROR);
            }
            return responseMessageService.makeMessage(Message.NO_HAVE_ROOM);
        }
        return responseMessageService.makeMessage(Message.PARAMS_ERROR);
    }

    /**
     * 通过用户的id号，查找该用户关注的所有的空间的id号
     *
     * @param userId
     * @return
     */
    public ResponseMessage selectRoomFollowerByUserId(int userId) {
        return null;
    }

    /**
     * 通过用户的id号，查找该空间内是否含有该用户
     *
     * @param userId
     * @param roomId
     * @return
     */
    public boolean hasUser(int userId, int roomId) {
        if (userId > 0 && roomId > 0){
            if (userService.hasUser(userId) && roomService.hasRoom(roomId)){
                if (iRoomFollowerImpl.hasUser(userId, roomId)){
                    return true;
                }
            }
        }
        return false;
    }

    /**
     * 通过用户的id号，查找该空间内是否含有该用户
     *
     * @param userId
     * @param roomId
     * @return
     */
    public ResponseMessage hasUserByUserIdAndRoomId(int userId, int roomId) {
        if (userId > 0 && roomId > 0){
            if (userService.hasUser(userId)){
                if (roomService.hasRoom(roomId)){
                    if (iRoomFollowerImpl.hasUser(userId, roomId)){
                        return responseMessageService.makeMessage(Message.SUCCESS);
                    }
                    return responseMessageService.makeMessage(Message.NO_HAVE_USER);
                }
                return responseMessageService.makeMessage(Message.NO_HAVE_ROOM);
            }
            return responseMessageService.makeMessage(Message.NO_HAVE_USER);
        }
        return responseMessageService.makeMessage(Message.PARAMS_ERROR);
    }

    /**
     * 删除该用户关注的的某一空间
     *
     * @param userId
     * @param roomId
     * @param operator
     * @return
     */
    public ResponseMessage deleteRoomFollower(int userId, int roomId, int operator) {
        if (userId > 0 && roomId > 0 && operator > 0){
            if (userService.hasUser(userId)){
                if (roomService.hasRoom(roomId)){
                    /**
                     * 判断用户是否是管理员，是否有权限删除用户,并且不可以删除管理员
                     */
                    Room room = iRoomImpl.getRoomById(roomId);
                    if ((room.getCreatorId() == operator || userId == operator) && room.getCreatorId() != userId){
                        RoomFollower roomFollower = new RoomFollower();
                        roomFollower.setUserId(userId);
                        roomFollower.setRoomId(roomId);
                        iRoomFollowerImpl.deleteRoomFollower(roomFollower);
                        return responseMessageService.makeMessage(Message.SUCCESS);
                    }
                    return responseMessageService.makeMessage(Message.NO_PERMISSION_ERROR);
                }
                return responseMessageService.makeMessage(Message.NO_HAVE_ROOM);
            }
            return responseMessageService.makeMessage(Message.NO_HAVE_USER);
        }
        return responseMessageService.makeMessage(Message.PARAMS_ERROR);
    }
}
